package com.manelrodriguez.applications.ibex35.to;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Parcel;
import android.os.Parcelable;

import com.manelrodriguez.applications.ibex35.R;

public final class Empresa implements Parcelable {
	public static final String PREFS_NAME = "MyPrefs";
	public static final String EMPRESA = "EMPRESA";
	public static final String FAVORITES_IDS = "IDS_FAVORITOS";
	public static final String FAVORITES_EMPRESAS_IDS = "IDS_EMPRESA_FAVORITOS";
	
	private String StockSymbol;
	private String LastTradeAmount;
	private String LastTradeDateTime;
	private String StockChange;
	private String OpenAmount;
	private String DayHigh;
	private String DayLow;
	private String StockVolume;
	private String PrevCls;
	private String ChangePercent;
	private String FiftyTwoWeekRange;
	private String EarnPerShare;
	private String PE;
	private String CompanyName;
	private String QuoteError;
	
	public Empresa(){
		super();
	}
	public String getStockSymbol() {
		return StockSymbol;
	}
	public void setStockSymbol(String stockSymbol) {
		StockSymbol = stockSymbol;
	}
	public String getLastTradeAmount() {
		return LastTradeAmount;
	}
	public void setLastTradeAmount(String lastTradeAmount) {
		LastTradeAmount = lastTradeAmount;
	}
	public String getLastTradeDateTime() {
		return LastTradeDateTime;
	}
	public void setLastTradeDateTime(String lastTradeDateTime) {
		LastTradeDateTime = lastTradeDateTime;
	}
	public String getStockChange() {
		return StockChange;
	}
	public void setStockChange(String stockChange) {
		StockChange = stockChange;
	}
	public String getOpenAmount() {
		return OpenAmount;
	}
	public void setOpenAmount(String openAmount) {
		OpenAmount = openAmount;
	}
	public String getDayHigh() {
		return DayHigh;
	}
	public void setDayHigh(String dayHigh) {
		DayHigh = dayHigh;
	}
	public String getDayLow() {
		return DayLow;
	}
	public void setDayLow(String dayLow) {
		DayLow = dayLow;
	}
	public String getStockVolume() {
		return StockVolume;
	}
	public void setStockVolume(String stockVolume) {
		StockVolume = stockVolume;
	}
	public String getPrevCls() {
		return PrevCls;
	}
	public void setPrevCls(String prevCls) {
		PrevCls = prevCls;
	}
	public String getChangePercent() {
		return ChangePercent;
	}
	public void setChangePercent(String changePercent) {
		ChangePercent = changePercent;
	}
	public String getFiftyTwoWeekRange() {
		return FiftyTwoWeekRange;
	}
	public void setFiftyTwoWeekRange(String fiftyTwoWeekRange) {
		FiftyTwoWeekRange = fiftyTwoWeekRange;
	}
	public String getEarnPerShare() {
		return EarnPerShare;
	}
	public void setEarnPerShare(String earnPerShare) {
		EarnPerShare = earnPerShare;
	}
	public String getPE() {
		return PE;
	}
	public void setPE(String pE) {
		PE = pE;
	}
	public String getCompanyName() {
		return CompanyName;
	}
	public void setCompanyName(String companyName) {
		CompanyName = companyName;
	}
	public String getQuoteError() {
		return QuoteError;
	}
	public void setQuoteError(String quoteError) {
		QuoteError = quoteError;
	}
	
	public int describeContents() {

		return 0;
	}
	public void writeToParcel(Parcel out, int flags) {
		out.writeString(StockSymbol);
		out.writeString(LastTradeAmount);
		out.writeString(LastTradeDateTime);
		out.writeString(StockChange);
		out.writeString(OpenAmount);
		out.writeString(DayHigh);
		out.writeString(DayLow);
		out.writeString(StockVolume);
		out.writeString(PrevCls);
		out.writeString(ChangePercent);
		out.writeString(FiftyTwoWeekRange);
		out.writeString(EarnPerShare);
		out.writeString(PE);
		out.writeString(CompanyName);
		out.writeString(QuoteError);
	}
	
	// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<Empresa> CREATOR = new Parcelable.Creator<Empresa>() {
        public Empresa createFromParcel(Parcel in) {
            return new Empresa(in);
        }

        public Empresa[] newArray(int size) {
            return new Empresa[size];
        }
    };

    // example constructor that takes a Parcel and gives you an object populated with it's values
    private Empresa(Parcel in) {
    	StockSymbol = in.readString();
    	LastTradeAmount = in.readString();
    	LastTradeDateTime = in.readString();
    	StockChange = in.readString();
    	OpenAmount = in.readString();
    	DayHigh = in.readString();
    	DayLow = in.readString();
    	StockVolume = in.readString();
    	PrevCls = in.readString();
    	ChangePercent = in.readString();
    	FiftyTwoWeekRange = in.readString();
    	EarnPerShare = in.readString();
    	PE = in.readString();
    	CompanyName = in.readString();
    	QuoteError = in.readString();
    }

    public boolean addFavorite(int posicion, Context context){
    	boolean add = true;
    	SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        String idFavoritos = settings.getString(FAVORITES_IDS, "");
        String idEmpresasFavoritas = settings.getString(FAVORITES_EMPRESAS_IDS, "");
        if(idFavoritos.equals("")){
        	idFavoritos = String.valueOf(posicion);
        	idEmpresasFavoritas = StockSymbol;
        }else{
        	for(String idEmpresa:idFavoritos.split("#")){
        		int intIdEmpresa = Integer.valueOf(idEmpresa);
        		if(intIdEmpresa == posicion)
        		{
        			add = false;
        		}
        	}
        	idFavoritos = idFavoritos + "#" + String.valueOf(posicion);
        	idEmpresasFavoritas = idEmpresasFavoritas + "#" + StockSymbol;
        }
        if(add){
        	editor.putString(FAVORITES_IDS, idFavoritos);
        	editor.putString(FAVORITES_EMPRESAS_IDS, idEmpresasFavoritas);
        	editor.commit();
        	return true;
        }else{
        	return false;
        }
    }
    
    
    public void borrarFavorito(int posicion, Context context){
    	SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		String idFavoritos = settings.getString(FAVORITES_IDS, "");
		String idEmpresaFavorito = settings.getString(FAVORITES_EMPRESAS_IDS, "");
		String aux = "";
		String aux2 = "";
		if(idFavoritos!= null && !idFavoritos.equals("")){
			String[] ids = idFavoritos.split("#");
	    	for(int i = 0;i<ids.length;i++){
	    		if(i != posicion){
	    			if(aux.equals("")){
	    				aux = ids[i];
	    			}else{
	    				aux = aux + "#" + ids[i];
	    			}
	    		}
	    	}
	    }
		if(idEmpresaFavorito!= null && !idEmpresaFavorito.equals("")){
			String[] idsempresas = idFavoritos.split("#");
	    	for(int i = 0;i<idsempresas.length;i++){
	    		if(StockSymbol != idsempresas[i]){
	    			if(aux2.equals("")){
	    				aux2 = idsempresas[i];
	    			}else{
	    				aux2 = aux2 + "#" + idsempresas[i];
	    			}
	    		}
	    	}
	    }
		editor.putString(FAVORITES_IDS, aux);
		editor.putString(FAVORITES_EMPRESAS_IDS, aux2);
        editor.commit();
	}
    
    public boolean isFavorite(Context context){
    	boolean isFavorito = false;
    	
    	SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    	String idEmpresasFavoritas = settings.getString(FAVORITES_IDS, "");
    	if(!idEmpresasFavoritas.equals("")){
    		for(String idEmpresa:idEmpresasFavoritas.split("#")){
        		if(idEmpresa.equals(StockSymbol))
        		{
        			isFavorito = true;
        		}
        	}
        }
    	
    	return isFavorito;
    }
    
    public static String getCodeString(String empresa){
		if(empresa.equals("Abertis")) return "ABE.MC";
		else if(empresa.equals("IBEX35")) return "^IBEX";
		else if(empresa.equals("Abengoa")) return "ABG-P.MC";
		else if(empresa.equals("ACS")) return "ACS.MC";
		else if(empresa.equals("Acerinox")) return "ACX.MC";
		else if(empresa.equals("Amadeus")) return "AMS.MC";
		else if(empresa.equals("Acciona")) return "ANA.MC";
		else if(empresa.equals("BBVA")) return "BBVA.MC";
		else if(empresa.equals("Dia")) return "DIA.MC";
		else if(empresa.equals("Bankinter")) return "BKT.MC";
		else if(empresa.equals("BME")) return "BME.MC";
		else if(empresa.equals("Caixabank")) return "CABK.MC";
		else if(empresa.equals("Ebro Foods")) return "EBRO.MC";
		else if(empresa.equals("Endesa")) return "ELE.MC";
		else if(empresa.equals("Enagas")) return "ENG.MC";
		else if(empresa.equals("FCC")) return "FCC.MC";
		else if(empresa.equals("Ferrovial")) return "FER.MC";
		else if(empresa.equals("Gas Natural")) return "GAS.MC";
		else if(empresa.equals("Grifols")) return "GRF.MC";
		else if(empresa.equals("IAG")) return "IAG.MC";
		else if(empresa.equals("Iberdrola")) return "IBE.MC";
		else if(empresa.equals("Indra")) return "IDR.MC";
		else if(empresa.equals("Inditex")) return "ITX.MC";
		else if(empresa.equals("Mapfre")) return "MAP.MC";
		else if(empresa.equals("Arcelormittal")) return "MTS.MC";
		else if(empresa.equals("OHL")) return "OHL.MC";
		else if(empresa.equals("Banco Popular")) return "POP.MC";
		else if(empresa.equals("REE")) return "REE.MC";
		else if(empresa.equals("Repsol YPF")) return "REP.MC";
		else if(empresa.equals("Banco Sabadell")) return "SAB.MC";
		else if(empresa.equals("Banco Santander")) return "SAN.MC";
		else if(empresa.equals("Sacyr Vallehermoso")) return "SYV.MC";
		else if(empresa.equals("Telefonica")) return "TEF.MC";
		else if(empresa.equals("Telecinco")) return "TL5.MC";
		else if(empresa.equals("Tecnicas Reunidas")) return "TRE.MC";
		else if(empresa.equals("Viscofan")) return "VIS.MC";
		else return "";
    }
    
    public static String getIdEmpresaString(String companyName, Context ctx){
    	if(companyName.equals("^IBEX")) return ctx.getString(R.string.IBEX35);
    	else if(companyName.equals("ABE.MC")) return ctx.getString(R.string.ABE_MC);
		else if(companyName.equals("ABG-P.MC")) return ctx.getString(R.string.ABG_MC);
		else if(companyName.equals("ACS.MC")) return ctx.getString(R.string.ACS_MC);
		else if(companyName.equals("ACX.MC")) return ctx.getString(R.string.ACX_MC);
		else if(companyName.equals("AMS.MC")) return ctx.getString(R.string.AMS_MC);
		else if(companyName.equals("ANA.MC")) return ctx.getString(R.string.ANA_MC);
		else if(companyName.equals("BBVA.MC")) return ctx.getString(R.string.BBVA_MC);
		else if(companyName.equals("DIA.MC")) return ctx.getString(R.string.DIA_MC);
		else if(companyName.equals("BKT.MC")) return ctx.getString(R.string.BKT_MC);
		else if(companyName.equals("BME.MC")) return ctx.getString(R.string.BME_MC);
		else if(companyName.equals("CABK.MC")) return ctx.getString(R.string.CABK_MC);
		else if(companyName.equals("EBRO.MC")) return ctx.getString(R.string.EBRO_MC);
		else if(companyName.equals("ELE.MC")) return ctx.getString(R.string.ELE_MC);
		else if(companyName.equals("ENG.MC")) return ctx.getString(R.string.ENG_MC);
		else if(companyName.equals("FCC.MC")) return ctx.getString(R.string.FCC_MC);
		else if(companyName.equals("FER.MC")) return ctx.getString(R.string.FER_MC);
		else if(companyName.equals("GAS.MC")) return ctx.getString(R.string.GAS_MC);
		else if(companyName.equals("GRF.MC")) return ctx.getString(R.string.GRF_MC);
		else if(companyName.equals("IAG.MC")) return ctx.getString(R.string.IAG_MC);
		else if(companyName.equals("IBE.MC")) return ctx.getString(R.string.IBE_MC);
		else if(companyName.equals("IDR.MC")) return ctx.getString(R.string.IDR_MC);
		else if(companyName.equals("ITX.MC")) return ctx.getString(R.string.ITX_MC);
		else if(companyName.equals("MAP.MC")) return ctx.getString(R.string.MAP_MC);
		else if(companyName.equals("MTS.MC")) return ctx.getString(R.string.MTS_MC);
		else if(companyName.equals("OHL.MC")) return ctx.getString(R.string.OHL_MC);
		else if(companyName.equals("POP.MC")) return ctx.getString(R.string.POP_MC);
		else if(companyName.equals("REE.MC")) return ctx.getString(R.string.REE_MC);
		else if(companyName.equals("REP.MC")) return ctx.getString(R.string.REP_MC);
		else if(companyName.equals("SAB.MC")) return ctx.getString(R.string.SAB_MC);
		else if(companyName.equals("SAN.MC")) return ctx.getString(R.string.SAN_MC);
		else if(companyName.equals("SYV.MC")) return ctx.getString(R.string.SYV_MC);
		else if(companyName.equals("TEF.MC")) return ctx.getString(R.string.TEF_MC);
		else if(companyName.equals("TL5.MC")) return ctx.getString(R.string.TL5_MC);
		else if(companyName.equals("TRE.MC")) return ctx.getString(R.string.TRE_MC);
		else if(companyName.equals("VIS.MC")) return ctx.getString(R.string.VIS_MC);
		else return companyName;
    	
    }
    
}
